﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RenderEngineInterfaces;
using System.ComponentModel.Composition;
using System.IO;
using System.Threading.Tasks;
using DeepZoomPivotConstructor.VisualLib;
using System.Drawing;
using DeepZoomPivotConstructor.ErrorAndLoggingInterfaces;

namespace SimpleFileRendering
{
    /// <summary>
    /// Render a FileInfo that points to ajpeg file
    /// </summary>
    [Export(typeof(IRenderItem))]
    [RenderInfo(typeof(FileInfo))]
    public class JPEGFileRender : IRenderItem
    {
        /// <summary>
        /// The DPI setting that we should be importing images at.
        /// </summary>
        public int DPI { get; set; }

        /// <summary>
        /// Error logger to get the message out if we have a failure here!
        /// </summary>
#pragma warning disable 0649
        [Import(AllowDefault = true)]
        private IErrorLogger MyLogger;
#pragma warning restore 0649

        /// <summary>
        /// Init our DPI to a default value.
        /// </summary>
        public JPEGFileRender()
        {
            DPI = 150;
        }

        /// <summary>
        /// We put a jpeg that we find on the ground into a visual...
        /// Since this involves opening files on the file system, we do this
        /// in a task.
        /// </summary>
        /// <param name="what"></param>
        /// <returns></returns>
        public Task<Visual> Render(object what)
        {
            return Task<Visual>.Factory.StartNew(() =>
                {
                    FileInfo f = what as FileInfo;

                    ///
                    /// Make sure the file is ok
                    /// 

                    if (!CheckFileOK(f))
                    {
                        if (MyLogger != null)
                        {
                            MyLogger.LogMessage("JPEGFileRender", "File " + f.FullName + " failed its ChecKFileOK call... ignoring it!");
                        }
                        return null;
                    }

                    ///
                    /// Ok! Build up the file as a visual!
                    /// 

                    var v = new VisualSingleImage(f, DPI);
                    v.Info = new Visual.VisualInfo();
                    v.Info.Title = f.Name.Substring(0, f.Name.Length - f.Extension.Length);

                    return v;
                }
            );
        }

        /// <summary>
        /// Looks at the file and makes sure it is ok.
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        private bool CheckFileOK(FileInfo f)
        {
            using (var img = Image.FromFile(f.FullName))
            {
                return img.HorizontalResolution == img.VerticalResolution;
            }
        }

        /// <summary>
        /// If this what is a string that is a valid path pointing to a real file, then
        /// we can go for it! But only generically. :-)
        /// </summary>
        /// <param name="what"></param>
        /// <returns></returns>
        public RenderPluginPriority RenderPriority(object what)
        {
            FileInfo f = what as FileInfo;
            if (!f.Exists)
            {
                return RenderPluginPriority.RPPCantDoIt;
            }

            string ext = f.Extension.ToLower();
            if (ext == ".jpeg" || ext == ".jpg")
            {
                return RenderPluginPriority.RPPGeneric;
            }
            if (ext == ".png")
            {
                return RenderPluginPriority.RPPGeneric;
            }
            return RenderPluginPriority.RPPCantDoIt;
        }
    }
}
